package zmee.pur.src.formplugin;

import com.alibaba.druid.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.db.tx.TX;
import kd.bos.db.tx.TXHandle;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.EventObject;

/**
 * @Author: xw
 * @Date: 2022/12/14/16:19
 * @Description: 标书编制【是否发送公告】为否自动提交、审核 【招标公告】、【中标公告】
 */
public class NoticeSubmitAndAuditBillPlugin extends AbstractBillPlugIn {

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        String billStatus = (String) this.getModel().getValue("billstatus");
        if (StringUtils.equals("A", billStatus)) {
            Object id = this.getModel().getValue("id");
            DynamicObject bidDoc = BusinessDataServiceHelper.loadSingle(id, "src_biddoc");
            if (bidDoc == null) {
                return;
            }
            boolean isPaper = bidDoc.getBoolean("zmee_ispaper");
            if (!isPaper) {
                String billName = this.getModel().getDataEntityType().getName();
                try (TXHandle tx = TX.requiresNew()) {
                    try {
                        OperationResult submitResult = OperationServiceHelper.executeOperate("submit", billName, new Object[]{id},
                                OperateOption.create());
                        if (submitResult.isSuccess()) {
                            OperationResult auditResult = OperationServiceHelper.executeOperate("audit", billName, new Object[]{id},
                                    OperateOption.create());
                            if (auditResult.isSuccess()) {
                                this.getView().invokeOperation("nextnode");
                            } else {
                                tx.markRollback();
                            }
                        } else {
                            tx.markRollback();
                        }
                    } catch (Throwable t) {
                        tx.markRollback();
                        throw t;
                    }
                }
            }
        }
    }
}
